package com.donica.wamstest.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.entity.TaskFindVO;
import com.donica.wamstest.entity.TaskStartVO;
import com.donica.wamstest.entity.TaskVO;
import com.donica.wamstest.service.LogService;
import com.donica.wamstest.service.MessageService;
import com.donica.wamstest.service.TaskService;
import com.donica.wamstest.utils.LogUtil;

import io.swagger.annotations.Api;

/*
 * @author laifuying
 * @version 1.0
 * @date  2020/2/9 19:33
 * @description 任务控制器
 */
@RestController
@RequestMapping("/api/v1/")
@Api(value = "taskController", tags = "任务控制器")
public class TaskController {
    @Autowired
    private TaskService taskService;
    @Autowired 
    private MessageService messageService;
	@Autowired
    private LogService logService;
	
    @RequestMapping(value = "mqtt/connection", method = RequestMethod.GET)
	public ReturnMessage getMqttConnection() {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			boolean result = messageService.isMqttConnected();
			rtnMsg.setResult(result);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}
    
    // -----任务管理--------
 	// 添加任务
 	@RequestMapping(value = "tasks", method = RequestMethod.POST)
 	public ReturnMessage InsertTask(@RequestBody TaskStartVO vo) {
 		ReturnMessage rtnMsg = new ReturnMessage();
 		try {

 			long count = messageService.startTask(vo);
 			rtnMsg.setResult(count);
 		} catch (Exception e) {
 			rtnMsg.setCode(ReturnMessage.FAIL);
 			rtnMsg.setMsg(e.toString());
 		}

 		logService.SaveLog(LogUtil.ACTION_ADD_TASK, rtnMsg.getCode(),rtnMsg.getMsg(), vo);
 		return rtnMsg;
 	}
 	
 	@RequestMapping(value = "tasks", method = RequestMethod.PUT)
	public ReturnMessage UpdateTask(@RequestBody List<TaskVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = taskService.UpdateTask(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	@RequestMapping(value = "tasks", method = RequestMethod.DELETE)
	public ReturnMessage DeleteTask(@RequestBody List<TaskVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = taskService.deleteTask(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	@RequestMapping(value = "tasks/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchTask(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody TaskFindVO vo) {
		int firstIndex = pageSize * currPage;
		vo.setFirstIndex(firstIndex);
		vo.setPageSize(pageSize);
		vo.setCurrPage(currPage);
		return taskService.searchTask(vo);
	}

}
